home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-06-22 | 8.1 KB | 305 lines | [TEXT/CWIE] |
- #include "DrawSprocket.h"
- #include "FlockDrawing.h"
- #include "FinderRegistry.h"
-
- /*
- ********************************************************************************
- ** globals
- ********************************************************************************
- */
- GrafPtr gOldPort;
- DSpContextReference gDrawContext;
- DSpAltBufferReference gUnderlay;
- extern long gNumItems;
- extern Handle gIcons[];
- extern Handle gMasks[];
- extern Point gPositions[];
- extern Rect gWindowRect;
- extern long gWindowView;
-
- // Local prototype
- void MyInitAttributes( DSpContextAttributes *inAttributes);
-
- /*
- ********************************************************************************
- ** SetUpDrawing
- ********************************************************************************
- */
- OSErr SetUpDrawing(RgnHandle worldRgn)
- {
- DSpContextAttributes theDesiredAttributes;
- OSStatus theError;
- short desiredWidth = 1024;
- short desiredHeight = 768;
- short cnt, rectsize = (gWindowView == pIconBitmap) ? 32 : 16;
- GDHandle mainDevice;
- Rect mainRect, rect;
-
-
- CGrafPtr theAltBufferPort;
- GDHandle theAltBufferGDevice, theOldGDevice;
- GrafPtr theOldPort;
- short rectSize = 16;
-
- if(worldRgn == nil)
- return memFullErr;
-
- // Get the info about the main device
- mainDevice = GetMainDevice();
- mainRect = (**mainDevice).gdRect;
- desiredWidth = mainRect.right - mainRect.left;
- desiredHeight = mainRect.bottom - mainRect.top;
-
- /* set the context */
- MyInitAttributes( &theDesiredAttributes );
- theDesiredAttributes.displayWidth = desiredWidth;
- theDesiredAttributes.displayHeight = desiredHeight;
- theDesiredAttributes.colorNeeds = kDSpColorNeeds_Require;
- theDesiredAttributes.backBufferDepthMask = kDSpDepthMask_8;
- theDesiredAttributes.displayDepthMask = kDSpDepthMask_8;
- theDesiredAttributes.backBufferBestDepth = 8;
- theDesiredAttributes.displayBestDepth = 8;
- theDesiredAttributes.pageCount = 2;
- theError = DSpFindBestContext( &theDesiredAttributes, &gDrawContext );
- if( theError && kDSpContextNotFoundErr != theError )
- {
- return theError;
- }
- if( kDSpContextNotFoundErr == theError )
- {
- return theError;
- }
-
- // save the world rgn
- SetRectRgn(worldRgn, 0, 0, theDesiredAttributes.displayWidth, theDesiredAttributes.displayHeight);
-
- /*
- ** Here is where I need to OR in the value to use page flipping. If
- ** I had used the value when calling DSpFindBestContext() then it would
- ** have only considered displays that have page flipping hardware, but
- ** I want to run with software buffering too.
- */
- theDesiredAttributes.contextOptions |= kDSpContextOption_PageFlip;
-
- /* reserve the context */
- theError = DSpContext_Reserve( gDrawContext, &theDesiredAttributes );
- if( theError )
- {
- return theError;
- }
-
- /*
- ** allocate an alt buffer that will be used for the underlay. An
- ** underlay is useful in games that have a need to restore from a
- ** static background.
- */
- theError = DSpAltBuffer_New( gDrawContext, false, 0, &gUnderlay );
- if( theError )
- {
- DSpContext_Release( gDrawContext );
- return theError;
- }
-
- theError = DSpContext_SetUnderlayAltBuffer( gDrawContext, gUnderlay );
- if( theError )
- {
- DSpContext_Release( gDrawContext );
- return theError;
- }
-
- // Capture the desktop into the alt buffer
- GetPort( &theOldPort );
- theOldGDevice = GetGDevice();
-
- /* set the alt buffer to be the current port */
- theError = DSpAltBuffer_GetCGrafPtr( gUnderlay,
- kDSpBufferKind_Normal, &theAltBufferPort, &theAltBufferGDevice );
- if( theError )
- {
- DSpContext_Release( gDrawContext );
- return theError;
- }
-
- /*
- ** if you want to use QuickDraw or any other toolbox rendering
- ** code, you must setup the GDevice as well as the port when
- ** working with AltBuffers!
- */
- SetPort( (GrafPtr)theAltBufferPort );
- SetGDevice( theAltBufferGDevice );
-
- /*
- ** fill the underlay buffer with the screen image
- */
- CopyBits(&qd.screenBits, &((GrafPtr)theAltBufferPort)->portBits,
- &theAltBufferPort->portRect, &theAltBufferPort->portRect, srcCopy, nil);
-
- // Erase all the icons that will be flocking, clipping to the window rect
- for(cnt = 0; cnt < gNumItems; cnt++)
- {
- SetRect(&rect, gPositions[cnt].h, gPositions[cnt].v,
- gPositions[cnt].h + rectsize, gPositions[cnt].v + rectsize);
- SectRect(&rect, &gWindowRect, &rect);
- EraseRect(&rect);
- }
-
- // Restore port
- SetPort( theOldPort );
- SetGDevice( theOldGDevice );
-
-
- // Set debug mode
- //DSpSetDebugMode(true);
-
- /* put the context into the active state */
- theError = DSpContext_SetState( gDrawContext, kDSpContextState_Active );
- if( theError )
- {
- DSpContext_Release( gDrawContext );
- return theError;
- }
-
- return noErr;
- }
-
- /*
- ********************************************************************************
- ** CloseDownDrawing
- ********************************************************************************
- */
- OSErr CloseDownDrawing(void)
- {
- OSStatus theError = 0;
-
- /* fade to black */
- //theError = DSpContext_FadeGammaOut( NULL, NULL );
- //if( theError )
- {
- DSpContext_Release( gDrawContext );
- return theError;
- }
-
- // remove and dispose underlay
- DSpContext_SetUnderlayAltBuffer( gDrawContext, NULL );
- DSpAltBuffer_Dispose( gUnderlay );
- gUnderlay = nil;
-
- /* put the context into the inactive state */
- theError = DSpContext_SetState( gDrawContext, kDSpContextState_Inactive );
- if( theError )
- {
- DSpContext_FadeGammaIn( NULL, NULL );
- DSpContext_Release( gDrawContext );
- return theError;
- }
-
- /* fade back in */
- //theError = DSpContext_FadeGammaIn( NULL, NULL );
- //if( theError )
- {
- DSpContext_Release( gDrawContext );
- return theError;
- }
-
- /* release the context */
- theError = DSpContext_Release( gDrawContext );
- if( theError )
- {
- return theError;
- }
- return noErr;
- }
-
- /*
- ********************************************************************************
- ** StartDrawing
- ********************************************************************************
- */
- OSErr StartDrawing()
- {
- Rect theRect;
- OSErr theError;
- CGrafPtr theAltBufferPort;
-
- GetPort( &gOldPort );
-
- /* set the alt buffer to be the current port */
-
- theError = DSpContext_GetBackBuffer( gDrawContext,
- kDSpBufferKind_Normal, &theAltBufferPort);
- if( theError )
- {
- DSpContext_Release( gDrawContext );
- return theError;
- }
-
- /*
- ** if you want to use QuickDraw or any other toolbox rendering
- ** code, you must setup the GDevice as well as the port when
- ** working with AltBuffers!
- */
- SetPort( (GrafPtr)theAltBufferPort );
- return noErr;
- }
-
- /*
- ********************************************************************************
- ** EndDrawing
- ********************************************************************************
- */
- OSErr EndDrawing()
- {
- OSErr theError;
-
- // Swap buffers and restore old port
- theError = DSpContext_SwapBuffers( gDrawContext, NULL, 0 );
- SetPort( gOldPort );
- return theError;
- }
-
- void MarkRect(Rect *rect)
- {
- // we ignore the error, if any.
- DSpContext_InvalBackBufferRect(gDrawContext, rect);
- }
-
- /*
- ********************************************************************************
- **
- ** Name: MyInitAttributes
- **
- ** Description:
- **
- ** Initialize a context attributes structure so that there is no garbage
- ** data in it. This is important to do because DS will return an error
- ** if some fields are set incorrectly (including the "reserved" fields not
- ** being set to zero), and some things can actually cause a crash (such
- ** as a bogus color table handle).
- **
- ********************************************************************************
- */
- void MyInitAttributes( DSpContextAttributes *inAttributes)
- {
- if( NULL == inAttributes )
- DebugStr("\pStimpy! You Idiot!");
-
- inAttributes->frequency = 0;
- inAttributes->displayWidth = 0;
- inAttributes->displayHeight = 0;
- inAttributes->reserved1 = 0;
- inAttributes->reserved2 = 0;
- inAttributes->colorNeeds = 0;
- inAttributes->colorTable = NULL;
- inAttributes->contextOptions = 0;
- inAttributes->backBufferDepthMask = 0;
- inAttributes->displayDepthMask = 0;
- inAttributes->backBufferBestDepth = 0;
- inAttributes->displayBestDepth = 0;
- inAttributes->pageCount = 0;
- inAttributes->gameMustConfirmSwitch = false;
- inAttributes->reserved3[0] = 0;
- inAttributes->reserved3[1] = 0;
- inAttributes->reserved3[2] = 0;
- inAttributes->reserved3[3] = 0;
- }
-